﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Contacts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadLogin();
            loadGroup();
        }
    }

   
    private void loadLogin()
    {
        if (hfLoginID.Value == "")
        {
            hfLoginID.Value = getLogin().ToString();
        }
    }

    private string getLoginID()
    {
        return hfLoginID.Value.Substring(6, hfLoginID.Value.Length - 6).Substring(0, hfLoginID.Value.Length - 12);

    }
    private string getLogin()
    {
        string loginID="";
        try
        {
            if (Session["OnlineSMS_LoginID"] != null)
            {
                loginID = Session["OnlineSMS_LoginID"].ToString();
            }
            else if (hfLoginID.Value != "")
            {
                loginID = hfLoginID.Value;
            }
            else
            { 
                Session["PreviousPage"] = HttpContext.Current.Request.Url.AbsoluteUri; Response.Redirect("Default.aspx"); }

        }
        catch (Exception ex)
        { }

        return loginID;
    }
   
    private void loadGroup()
    {
        string sql = @"
SELECT  [GroupID]
      ,[GroupName]
      ,[dbo].[GetContactCountyLoginID](GroupID) as NoOfContacts
  FROM [Group] where StatusID=1 and LoginID=" + getLoginID() + @"
order by GroupName
";

        DataSet ds = DatabaseManager.ExecSQL(sql);

        gvGroup.DataSource = ds.Tables[0];
        gvGroup.DataBind();

        
    }

    protected void lbSelect_Click(object sender, EventArgs e)
    {
        LinkButton linkButton = new LinkButton();
        linkButton = (LinkButton)sender;
        DataSet ds = DatabaseManager.ExecSQL("select * from [Group] where GroupID=" + linkButton.CommandArgument);
        txtGroupName.Text = ds.Tables[0].Rows[0]["GroupName"].ToString();
        hfGroupID.Value = ds.Tables[0].Rows[0]["GroupID"].ToString();
    }
    protected void lbDeleteGroup_Click(object sender, EventArgs e)
    {
        LinkButton linkButton = new LinkButton();
        linkButton = (LinkButton)sender;
        DatabaseManager.ExecSQL("Update [Group] set StatusID=0 where GroupID=" + linkButton.CommandArgument);
        loadGroup();
    }
    

    protected void btnSaveGroup_Click(object sender, EventArgs e)
    {
        if (hfGroupID.Value != "")
        {
            DatabaseManager.ExecSQL("Update [Group] set GroupName='" + txtGroupName.Text + @"' where GroupID=" + hfGroupID.Value);
            hfGroupID.Value = "";
            loadGroup();
        }
        else
        {
            DatabaseManager.ExecSQL(@"
INSERT INTO [Group]
           ([GroupName]
           ,[LoginID]
           ,[AddedDate]
           ,[StatusID])
     VALUES
           ('"+txtGroupName.Text+@"'
           ," + getLoginID() + @"
           ,Getdate()
           ,1);
");
            loadGroup();
        }
        txtGroupName.Text = "";
    }
    
}